//
//  USFlagByLayer.m
//  iOSStudy
//
//  Created by Evan Xie on 12/3/15.
//  Copyright © 2015 Family Wings. All rights reserved.
//

#import "USFlagByLayer.h"


#define kStripeHeight    20.0
#define kStripeCount     7
#define kStarWidth      20.0
#define kStarHeight     17
#define kSixStarsInRow  6
#define kFiveStarsInRow 5

@implementation USFlagByLayer


- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetAllowsAntialiasing(context, true);
    CGContextSetShouldAntialias(context, true);
    
    // Make background is white.
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextFillRect(context, rect);
    
    // Create stripe layer
    CGRect stripeRect  = CGRectMake (0, 0, rect.size.width, kStripeHeight);
    CGLayerRef stripeLayer = CGLayerCreateWithContext(context, stripeRect.size, NULL);
    CGContextRef stripeContext = CGLayerGetContext(stripeLayer);
    CGContextSetFillColorWithColor(stripeContext, [UIColor redColor].CGColor);
    CGContextFillRect(stripeContext, stripeRect);
    
    // Create star layer
    const CGPoint starPoints[] = {{ 5, 5},   {10, 15},
        {10, 15},  {15, 5},
        {15, 5},   {2.5, 11},
        {2.5, 11}, {16.5, 11},
        {16.5, 11},{5, 5}};
    
    CGRect starRect = CGRectMake (0, 0, kStarWidth, kStarHeight);
    
    CGLayerRef starLayer = CGLayerCreateWithContext(context, starRect.size, NULL);
    CGContextRef starContext = CGLayerGetContext(starLayer);
    CGContextSetFillColorWithColor(starContext, [UIColor whiteColor].CGColor);
    CGContextAddLines(starContext, starPoints, 10);
    CGContextFillPath(starContext);
    
    CGContextSaveGState(context);
    // Draw 7 red strips
    for (int index = 0; index < kStripeCount; index++) {
        CGContextDrawLayerAtPoint(context, CGPointZero, stripeLayer);
        CGContextTranslateCTM(context, 0, 2*kStripeHeight);
    }
    CGContextRestoreGState(context);
        
    // Draw stars background
    CGFloat h_spacing = 10.0;
    CGFloat v_spacing = 0;
    CGRect starsFrame = CGRectMake(0, 0, kSixStarsInRow*kStarWidth+(kSixStarsInRow+1)*h_spacing, 9*kStarHeight+10*v_spacing);
    CGContextAddRect(context, starsFrame);
    CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor);
    CGContextFillRect(context, starsFrame);
    
    CGContextSaveGState(context);
    
    // Draw stars
    for (int index = 0; index < 5; index++) {
        // Draw 5 rows stars, which row contains 6 stars
        for (int j = 0; j < 6; j++) {
            CGPoint point = {h_spacing*(j+1) + kStarWidth*j, 2*kStarHeight*index};;
            CGContextDrawLayerAtPoint(context, point, starLayer);
        }
    }
    
    for (int index = 0; index < 4; index++) {
        // Draw 4 rows stars, which row contains 5 stars
        for (int j = 0; j < 5; j++) {
            CGPoint point = {(h_spacing+kStarWidth)*(j+1), 2*kStarHeight*index+kStarHeight};;
            CGContextDrawLayerAtPoint(context, point, starLayer);
        }
    }
    
    CGContextRestoreGState(context);
    
    CGLayerRelease(stripeLayer);
    CGLayerRelease(starLayer);
}

@end
